package net.hn.hnms.biz.plan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.hn.hnms.biz.plan.domain.ExecutionFootageOutput;
import net.hn.hnms.biz.plan.domain.PlanDrivage;
import net.hn.hnms.biz.plan.domain.PlanOutput;
import net.hn.hnms.biz.plan.mapper.ExecutionFootageOutputMapper;
import net.hn.hnms.biz.plan.mapper.PlanDrivageMapper;
import net.hn.hnms.biz.plan.mapper.PlanOutputMapper;
import net.hn.hnms.biz.plan.mapper.StatistacalAnalysisMapper;
import net.hn.hnms.biz.plan.request.StatisitcalAnalysisReq;
import net.hn.hnms.biz.plan.service.StatistcalAnalysisService;
import net.hn.hnms.biz.plan.vo.MonthOuputVO;
import net.hn.hnms.biz.plan.vo.StatisticalDayOutputVo;
import net.hn.hnms.biz.plan.vo.StatisticalYearVo;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Stream;

class StatisticalDayOUtputVoComparatro implements Comparator<StatisticalDayOutputVo> {

    @Override
    public int compare(StatisticalDayOutputVo o1, StatisticalDayOutputVo o2) {
        return o2.getDate().compareTo(o1.getDate());
    }
}

@Service
public class StatistcalAnalysisServiceImpl implements StatistcalAnalysisService {


    @Autowired
    private PlanOutputMapper outputPlanMapper;


    @Resource
    private PlanDrivageMapper planDrivageMapper;
    @Autowired
    private ExecutionFootageOutputMapper executionFootageOutputMapper;

    @Resource
    private StatistacalAnalysisMapper statistacalAnalysisMapper;

    private static final char c = 0;

    /**
     * 统计日日产量
     *
     * @param statisitcalAnalysisReq
     * @return
     */
    @Override
    public List<StatisticalDayOutputVo> list(StatisitcalAnalysisReq statisitcalAnalysisReq) {
        List<String> dateTimeRange = statisitcalAnalysisReq.getDateTimeRange();

        Date beginDate = statisitcalAnalysisReq.getBeginDate();
        Date endDate = statisitcalAnalysisReq.getEndDate();
        if (dateTimeRange == null || dateTimeRange.size() == 0) {
            LocalDate endDatet = LocalDate.now().plusDays(-1);
            LocalDate beginDatet = LocalDate.now().plusDays(-30);
            List<String> strings = new ArrayList<>();
            strings.add(beginDatet.toString());
            strings.add(endDatet.toString());
            statisitcalAnalysisReq.setDateTimeRange(strings);

        }
        List<String> betweenDate = getBetweenDate(statisitcalAnalysisReq.getBeginDate().toString(), statisitcalAnalysisReq.getEndDate().toString());

        List<StatisticalDayOutputVo> statisticalDayOutputVos = new ArrayList<>();
        if (betweenDate != null) {

            for (String date : betweenDate) {


                StatisticalDayOutputVo statisticalDayOutputVo = calculateOneResult(date, statisitcalAnalysisReq.getMineCode());

                statisticalDayOutputVos.add(statisticalDayOutputVo);

            }
        }
        Collections.sort(statisticalDayOutputVos, new StatisticalDayOUtputVoComparatro());


        return statisticalDayOutputVos;
    }

    /**
     * 获取两个日期间隔的所有日期
     *
     * @param start 格式必须为'2018-01-25'
     * @param end   格式必须为'2018-01-25'
     * @return
     */
    public List<String> getBetweenDate(String start, String end) {
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start.split(" ")[0]);
        LocalDate endDate = LocalDate.parse(end.split(" ")[0]);

        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance >= 30) {
            throw new HuaNengException(500, "开始日期与结束日期之间的天数不能大于30天");
        }
        if (distance < 1) {
            return list;
        }
        Stream.iterate(startDate, d -> {
            return d.plusDays(1);
        }).limit(distance + 1).forEach(f -> {
            list.add(f.toString());
        });
        return list;
    }

    /**
     * 获取一天的统计信息
     *
     * @param date
     * @return
     */
    private StatisticalDayOutputVo calculateOneResult(String date, String mineCode) {


        StatisticalDayOutputVo statisticalDayOutputVo = new StatisticalDayOutputVo();

        if (StringUtils.isBlank(date)) {
            throw new HuaNengException(500, "参数不正确");
        }
        //拿到这个月的天数
        LocalDate localDate = LocalDate.parse(date);
        statisticalDayOutputVo.setDate(localDate);
        int firstDay = localDate.with(TemporalAdjusters.firstDayOfMonth()).lengthOfMonth();

        //获取月计划拆分为日计划 产量
        String newDate = date.replace("-", "");
        String monthPlanCode = newDate.substring(0, newDate.length() - 2);
        QueryWrapper<PlanOutput> outputQueryWrapper = new QueryWrapper<>();
        outputQueryWrapper.eq("output_plan_type", "1").eq("output_plan_code", monthPlanCode);
        outputQueryWrapper.eq(StrUtil.isNotBlank(mineCode), "mine_code", mineCode);
        List<PlanOutput> planOutputs = outputPlanMapper.selectList(outputQueryWrapper);
        if (planOutputs == null || planOutputs.size() == 0) {
            statisticalDayOutputVo.setScheduledOutput("--");
        }
        BigDecimal scheduledOutput = null;
        if (planOutputs.size() > 0) {
            scheduledOutput = new BigDecimal(0);


            BigDecimal planCount = new BigDecimal(0);
            for (PlanOutput planOutput : planOutputs) {
                planCount = planCount.add(planOutput.getPlanOutput());

            }
            scheduledOutput = planCount.divide(BigDecimal.valueOf(firstDay), 2, RoundingMode.HALF_UP);
            statisticalDayOutputVo.setScheduledOutput(scheduledOutput + "");
        }
        //获取日实际 产量  m数    早中晚 三班之和 和不同工作面之和

        QueryWrapper<ExecutionFootageOutput> executionFootageOutputQueryWrapper = new QueryWrapper<>();
        executionFootageOutputQueryWrapper.eq(StrUtil.isNotBlank(mineCode), "mine_code", mineCode);
        LocalDate date1 = LocalDate.parse(date);
        executionFootageOutputQueryWrapper.eq("execution_date", date1).eq("work_face_type", "1").eq(StrUtil.isNotBlank(mineCode), "mine_code", mineCode);
        List<ExecutionFootageOutput> executionFootageOutputs = executionFootageOutputMapper.selectList(executionFootageOutputQueryWrapper);
        QueryWrapper<ExecutionFootageOutput> outputquery = new QueryWrapper<>();
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("0");
        outputquery.eq("execution_date", date1).eq(StrUtil.isNotBlank(mineCode), "mine_code", mineCode).and(o ->o.eq("work_face_type", "0").or().eq("work_face_type", "1"))
        ;
        List<ExecutionFootageOutput> executionOutputs = executionFootageOutputMapper.selectList(outputquery);

        //如果查詢到的集合爲空 證明為填報該天的回采进尺和米数
        if (executionFootageOutputs.size() == 0) {
            statisticalDayOutputVo.setFootage("--");
        }
        if (executionOutputs.size() == 0) {
            statisticalDayOutputVo.setOutput("--");
        }
        BigDecimal footageCount = null;
        BigDecimal outputCount = null;
        if (executionFootageOutputs != null && executionFootageOutputs.size() > 0) {

            footageCount = new BigDecimal(0);
            for (ExecutionFootageOutput executionFootageOutput : executionFootageOutputs) {
                BigDecimal footage = executionFootageOutput.getFootage();
                BigDecimal output = executionFootageOutput.getOutput();
                if (footage == null) {
                    footage = new BigDecimal(0);
                }
                footageCount = footageCount.add(footage);

            }
        }
        if (footageCount != null) {
            statisticalDayOutputVo.setFootage(footageCount + "");
        }

        if (executionOutputs != null && executionOutputs.size() > 0) {
            outputCount = new BigDecimal(0);
            for (ExecutionFootageOutput executionFootageOutput : executionOutputs) {
                BigDecimal output = executionFootageOutput.getOutput();
                if (output == null) {
                    output = new BigDecimal(0);
                }
                outputCount = outputCount.add(output);
            }
            if (outputCount != null) {
                statisticalDayOutputVo.setOutput(outputCount + "");
            }
        }


        ////回采计划月填报 早中晚 想加 除以本月天数获取 每天的回采米数
        QueryWrapper<PlanDrivage> planMiningQueryWrapper = new QueryWrapper<>();
        planMiningQueryWrapper.eq(StrUtil.isNotBlank(mineCode), "mine_code", mineCode);
        planMiningQueryWrapper.eq("drivage_plan_code", monthPlanCode).eq("drivage_plan_type", "1");
        List<PlanDrivage> planMinings = planDrivageMapper.selectList(planMiningQueryWrapper);
        if (planMinings == null || planMinings.size() == 0) {
            statisticalDayOutputVo.setScheduledFootage("--");
        }
        BigDecimal planMiningFootageDay = null;
        if (planMinings != null && planMinings.size() > 0) {
            planMiningFootageDay = new BigDecimal(0);
            BigDecimal planMiningCount = new BigDecimal(0);
            for (PlanDrivage planMining : planMinings) {
                BigDecimal miningPlanFootage = planMining.getDrivagePlanFootage();
                if (miningPlanFootage == null) {
                    miningPlanFootage = new BigDecimal(0);
                }
                planMiningCount = planMiningCount.add(miningPlanFootage);
            }
            planMiningFootageDay = planMiningCount.divide(BigDecimal.valueOf(firstDay), 2, RoundingMode.HALF_UP);
            statisticalDayOutputVo.setScheduledFootage(planMiningFootageDay + "");
        }

        //产量超欠
        if (outputCount == null || scheduledOutput == null) {
            statisticalDayOutputVo.setBackBreak("--");
        }
        if (outputCount != null && scheduledOutput != null) {
            BigDecimal output = outputCount.subtract(scheduledOutput);
            statisticalDayOutputVo.setBackBreak(output + "");
        }
        //进尺超欠
        if (footageCount == null || planMiningFootageDay == null) {
            statisticalDayOutputVo.setDayFootage("--");
        }
        if (footageCount != null && planMiningFootageDay != null) {
            BigDecimal dayFootage = footageCount.subtract(planMiningFootageDay);
            statisticalDayOutputVo.setDayFootage(dayFootage + "");
        }
        //产量完成率

        if (outputCount == null || scheduledOutput == null || scheduledOutput.compareTo(BigDecimal.ZERO) == 0) {
            statisticalDayOutputVo.setPercentage("--");
        }
        if (outputCount != null && scheduledOutput != null && scheduledOutput.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal percentage = outputCount.divide(scheduledOutput, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
            statisticalDayOutputVo.setPercentage(percentage + "%");
        }
        //进尺完成率
        if (footageCount == null || planMiningFootageDay == null || planMiningFootageDay.compareTo(BigDecimal.ZERO) == 0) {
            statisticalDayOutputVo.setFootagePercentage("--");
        }
        if (footageCount != null && planMiningFootageDay != null && planMiningFootageDay.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal footagePercentage = footageCount.divide(planMiningFootageDay, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
            statisticalDayOutputVo.setFootagePercentage(footagePercentage + "%");
        }
        return statisticalDayOutputVo;
    }

    /**
     * 年产量统计分析
     *
     * @param statisitcalAnalysisReq
     * @return
     */
    @Override
    public StatisticalYearVo listYearOutPut(StatisitcalAnalysisReq statisitcalAnalysisReq) {
        if (statisitcalAnalysisReq.getYear() == null) {
            statisitcalAnalysisReq.setYear(LocalDate.now().getYear() + "");
        }

        // 计算月产
        QueryWrapper<PlanOutput> outputQueryWrapper = new QueryWrapper<>();
        String year = statisitcalAnalysisReq.getYear();
        outputQueryWrapper.eq("output_plan_type", "1").like("output_plan_code", statisitcalAnalysisReq.getYear());
        outputQueryWrapper.eq(StrUtil.isNotBlank(statisitcalAnalysisReq.getMineCode()), "mine_code", statisitcalAnalysisReq.getMineCode());
        List<PlanOutput> planOutputs = outputPlanMapper.selectList(outputQueryWrapper);
        StatisticalYearVo statisticalYearVo = new StatisticalYearVo();
        if (planOutputs != null || planOutputs.size() > 0) {
            //没有查到该年的每月产量计划
            BigDecimal firstQuarter = new BigDecimal(0);
            BigDecimal secondQuarter = new BigDecimal(0);
            BigDecimal thirdQuarter = new BigDecimal(0);
            BigDecimal fourthQuarter = new BigDecimal(0);
            BigDecimal yearPlan = new BigDecimal(0);
            for (PlanOutput planOutput : planOutputs) {
                if ((year + "01").equals(planOutput.getOutputPlanCode())) {
                    firstQuarter = firstQuarter.add(planOutput.getPlanOutput());
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    statisticalYearVo.setJan(planOutput.getPlanOutput() + "");
                }
                if ((year + "02").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    firstQuarter = firstQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setFeb(planOutput.getPlanOutput() + "");
                }
                if ((year + "03").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    firstQuarter = firstQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setMar(planOutput.getPlanOutput() + "");
                }
                if ((year + "04").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    secondQuarter = secondQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setApr(planOutput.getPlanOutput() + "");
                }
                if ((year + "05").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    secondQuarter = secondQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setMay(planOutput.getPlanOutput() + "");
                }
                if ((year + "06").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    secondQuarter = secondQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setJun(planOutput.getPlanOutput() + "");
                }
                if ((year + "07").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    thirdQuarter = thirdQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setJul(planOutput.getPlanOutput() + "");
                }
                if ((year + "08").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    thirdQuarter = thirdQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setAug(planOutput.getPlanOutput() + "");
                }
                if ((year + "09").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    thirdQuarter = thirdQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setSept(planOutput.getPlanOutput() + "");
                }
                if ((year + "10").equals(planOutput.getOutputPlanCode())) {
                    fourthQuarter = fourthQuarter.add(planOutput.getPlanOutput());
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    statisticalYearVo.setOct(planOutput.getPlanOutput() + "");
                }
                if ((year + "11").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    fourthQuarter = fourthQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setNov(planOutput.getPlanOutput() + "");
                }
                if ((year + "12").equals(planOutput.getOutputPlanCode())) {
                    yearPlan = yearPlan.add(planOutput.getPlanOutput());
                    fourthQuarter = fourthQuarter.add(planOutput.getPlanOutput());
                    statisticalYearVo.setDec(planOutput.getPlanOutput() + "");
                }

            }
            if (!"--".equals(statisticalYearVo.getJan()) || !"--".equals(statisticalYearVo.getFeb()) || !"--".equals(statisticalYearVo.getMar())) {
                statisticalYearVo.setFirstQuarter(firstQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getApr()) || !"--".equals(statisticalYearVo.getMay()) || !"--".equals(statisticalYearVo.getJun())) {
                statisticalYearVo.setSecondQuarter(secondQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getJul()) || !"--".equals(statisticalYearVo.getAug()) || !"--".equals(statisticalYearVo.getSept())) {
                statisticalYearVo.setThirdQuarter(thirdQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getOct()) || !"--".equals(statisticalYearVo.getNov()) || !"--".equals(statisticalYearVo.getDec())) {
                statisticalYearVo.setFourthQuarter(fourthQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getFirstQuarter()) || !"--".equals(statisticalYearVo.getSecondQuarter()) || !"--".equals(statisticalYearVo.getThirdQuarter()) || "--".equals(statisticalYearVo.getFourthQuarter())) {
                statisticalYearVo.setYearPlan(yearPlan + "");
            }

        }


        //计算实际产量
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate beginDate = LocalDate.parse(year + "-01-01");
        LocalDate endDate = LocalDate.parse(year + "-12-31");
        ZonedDateTime beginDate1 = beginDate.atStartOfDay(zoneId);
        ZonedDateTime endDate1 = endDate.atStartOfDay(zoneId);

        Date begin = Date.from(beginDate1.toInstant());
        Date end = Date.from(endDate1.toInstant());



        List<MonthOuputVO> monthOuputVOS = statistacalAnalysisMapper.listMonthOutput(begin, end, statisitcalAnalysisReq.getMineCode());
        if (monthOuputVOS.size() > 0) {

            BigDecimal firstQuarterActual = new BigDecimal(0);
            BigDecimal secondQuarterActual = new BigDecimal(0);
            BigDecimal thirdQuarterActual = new BigDecimal(0);
            BigDecimal fourthQuarterActual = new BigDecimal(0);
            BigDecimal yearActual = new BigDecimal(0);
            for (MonthOuputVO monthOuputVO : monthOuputVOS) {
                //没有查到该年的每月产量计划
                if ((year + "01").equals(monthOuputVO.getYearMonth())) {
                    firstQuarterActual = firstQuarterActual.add(monthOuputVO.getOutput());
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setJanActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "02").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    firstQuarterActual = firstQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setFebActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "03").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    firstQuarterActual = firstQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setMarActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "04").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    secondQuarterActual = secondQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setAprActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "05").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    secondQuarterActual = secondQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setMayActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "06").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    secondQuarterActual = secondQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setJunActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "07").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    thirdQuarterActual = thirdQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setJulActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "08").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    thirdQuarterActual = thirdQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setAugActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "09").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    thirdQuarterActual = thirdQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setSeptActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "10").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    fourthQuarterActual = fourthQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setOctActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "11").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    fourthQuarterActual = fourthQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setNovActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "12").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    fourthQuarterActual = fourthQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setDecActual(monthOuputVO.getOutput() + "");
                }


            }
            if (!"--".equals(statisticalYearVo.getJanActual()) || !"--".equals(statisticalYearVo.getFebActual()) || !"--".equals(statisticalYearVo.getMarActual())) {
                statisticalYearVo.setFirstQuarterActual(firstQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getAprActual()) || !"--".equals(statisticalYearVo.getMayActual()) || !"--".equals(statisticalYearVo.getJunActual())) {
                statisticalYearVo.setSecondQuarterActual(secondQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getJulActual()) || !"--".equals(statisticalYearVo.getAugActual()) || !"--".equals(statisticalYearVo.getSeptActual())) {
                statisticalYearVo.setThirdQuarterActual(thirdQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getOctActual()) || !"--".equals(statisticalYearVo.getNovActual()) || !"--".equals(statisticalYearVo.getDecActual())) {
                statisticalYearVo.setFourthQuarterActual(fourthQuarterActual + "");
            }

            if (!"--".equals(statisticalYearVo.getFirstQuarterActual()) || !"--".equals(statisticalYearVo.getFirstQuarterActual()) || !"--".equals(statisticalYearVo.getFirstQuarterActual()) || !"--".equals(statisticalYearVo.getFirstQuarterActual())) {
                statisticalYearVo.setYearActual(yearActual + "");
            }




        }

        StatisticalYearVo statisticalYearVo1 = statisticalYearVo.calculatebackBreak(statisticalYearVo);


        return statisticalYearVo1;
    }


    /**
     * 年进尺统计分析
     *
     * @param statisitcalAnalysisReq
     * @return
     */
    @Override
    public StatisticalYearVo listYearFootage(StatisitcalAnalysisReq statisitcalAnalysisReq) {
        if (statisitcalAnalysisReq.getYear() == null) {
            statisitcalAnalysisReq.setYear(LocalDate.now().getYear() + "");
        }
        String year = statisitcalAnalysisReq.getYear();
        StatisticalYearVo statisticalYearVo = new StatisticalYearVo();

        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate beginDate = LocalDate.parse(year + "-01-01");
        LocalDate endDate = LocalDate.parse(year + "-12-31");
        ZonedDateTime beginDate1 = beginDate.atStartOfDay(zoneId);
        ZonedDateTime endDate1 = endDate.atStartOfDay(zoneId);

        Date begin = Date.from(beginDate1.toInstant());
        Date end = Date.from(endDate1.toInstant());

        //查询实际的每月掘进进尺（m）
        List<MonthOuputVO> monthOuputVOS = statistacalAnalysisMapper.listMonthfootage(begin, end, statisitcalAnalysisReq.getMineCode());
        if (monthOuputVOS.size() > 0) {

            BigDecimal firstQuarterActual = new BigDecimal(0);
            BigDecimal secondQuarterActual = new BigDecimal(0);
            BigDecimal thirdQuarterActual = new BigDecimal(0);
            BigDecimal fourthQuarterActual = new BigDecimal(0);
            BigDecimal yearActual = new BigDecimal(0);
            for (MonthOuputVO monthOuputVO : monthOuputVOS) {
                //没有查到该年的每月产量计划
                if ((year + "01").equals(monthOuputVO.getYearMonth())) {
                    firstQuarterActual = firstQuarterActual.add(monthOuputVO.getOutput());
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setJanActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "02").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    firstQuarterActual = firstQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setFebActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "03").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    firstQuarterActual = firstQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setMarActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "04").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    secondQuarterActual = secondQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setAprActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "05").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    secondQuarterActual = secondQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setMayActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "06").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    secondQuarterActual = secondQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setJunActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "07").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    thirdQuarterActual = thirdQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setJulActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "08").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    thirdQuarterActual = thirdQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setAugActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "09").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    thirdQuarterActual = thirdQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setSeptActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "10").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    fourthQuarterActual = fourthQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setOctActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "11").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    fourthQuarterActual = fourthQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setNovActual(monthOuputVO.getOutput() + "");
                }
                if ((year + "12").equals(monthOuputVO.getYearMonth())) {
                    yearActual = yearActual.add(monthOuputVO.getOutput());
                    fourthQuarterActual = fourthQuarterActual.add(monthOuputVO.getOutput());
                    statisticalYearVo.setDecActual(monthOuputVO.getOutput() + "");
                }


            }
            if (!"--".equals(statisticalYearVo.getJanActual()) || !"--".equals(statisticalYearVo.getFebActual()) || !"--".equals(statisticalYearVo.getMarActual())) {
                statisticalYearVo.setFirstQuarterActual(firstQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getAprActual()) || !"--".equals(statisticalYearVo.getMayActual()) || !"--".equals(statisticalYearVo.getJunActual())) {
                statisticalYearVo.setSecondQuarterActual(secondQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getJulActual()) || !"--".equals(statisticalYearVo.getAugActual()) || !"--".equals(statisticalYearVo.getSeptActual())) {
                statisticalYearVo.setThirdQuarterActual(thirdQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getOctActual()) || !"--".equals(statisticalYearVo.getNovActual()) || !"--".equals(statisticalYearVo.getDecActual())) {
                statisticalYearVo.setFourthQuarterActual(fourthQuarterActual + "");
            }
            if (!"--".equals(statisticalYearVo.getFirstQuarterActual()) || !"--".equals(statisticalYearVo.getFirstQuarterActual()) || !"--".equals(statisticalYearVo.getFirstQuarterActual()) || !"--".equals(statisticalYearVo.getFirstQuarterActual())) {
                statisticalYearVo.setYearActual(yearActual + "");
            }


        }


        //插叙月计划进尺
        List<MonthOuputVO> planOutputs = planDrivageMapper.planMonthfootage(begin, end, statisitcalAnalysisReq.getMineCode());
        if (planOutputs.size() > 0){
            //没有查到该年的每月产量计划
            BigDecimal firstQuarter = new BigDecimal(0);
            BigDecimal secondQuarter = new BigDecimal(0);
            BigDecimal thirdQuarter = new BigDecimal(0);
            BigDecimal fourthQuarter = new BigDecimal(0);
            BigDecimal yearPlan = new BigDecimal(0);
            for (MonthOuputVO planOutput : planOutputs) {
                if ((year + "01").equals(planOutput.getYearMonth())) {
                    firstQuarter = firstQuarter.add(planOutput.getOutput());
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    statisticalYearVo.setJan(planOutput.getOutput() + "");
                }
                if ((year + "02").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    firstQuarter = firstQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setFeb(planOutput.getOutput() + "");
                }
                if ((year + "03").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    firstQuarter = firstQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setMar(planOutput.getOutput() + "");
                }
                if ((year + "04").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    secondQuarter = secondQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setApr(planOutput.getOutput() + "");
                }
                if ((year + "05").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    secondQuarter = secondQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setMay(planOutput.getOutput() + "");
                }
                if ((year + "06").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    secondQuarter = secondQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setJun(planOutput.getOutput() + "");
                }
                if ((year + "07").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    thirdQuarter = thirdQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setJul(planOutput.getOutput() + "");
                }
                if ((year + "08").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    thirdQuarter = thirdQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setAug(planOutput.getOutput() + "");
                }
                if ((year + "09").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    thirdQuarter = thirdQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setSept(planOutput.getOutput() + "");
                }
                if ((year + "10").equals(planOutput.getYearMonth())) {
                    fourthQuarter = fourthQuarter.add(planOutput.getOutput());
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    statisticalYearVo.setOct(planOutput.getOutput() + "");
                }
                if ((year + "11").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    fourthQuarter = fourthQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setNov(planOutput.getOutput() + "");
                }
                if ((year + "12").equals(planOutput.getYearMonth())) {
                    yearPlan = yearPlan.add(planOutput.getOutput());
                    fourthQuarter = fourthQuarter.add(planOutput.getOutput());
                    statisticalYearVo.setDec(planOutput.getOutput() + "");
                }

            }
            if (!"--".equals(statisticalYearVo.getJan()) || !"--".equals(statisticalYearVo.getFeb()) || !"--".equals(statisticalYearVo.getMar())) {
                statisticalYearVo.setFirstQuarter(firstQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getApr()) || !"--".equals(statisticalYearVo.getMay()) || !"--".equals(statisticalYearVo.getJun())) {
                statisticalYearVo.setSecondQuarter(secondQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getJul()) || !"--".equals(statisticalYearVo.getAug()) || !"--".equals(statisticalYearVo.getSept())) {
                statisticalYearVo.setThirdQuarter(thirdQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getOct()) || !"--".equals(statisticalYearVo.getNov()) || !"--".equals(statisticalYearVo.getDec())) {
                statisticalYearVo.setFourthQuarter(fourthQuarter + "");
            }
            if (!"--".equals(statisticalYearVo.getFirstQuarter()) || !"--".equals(statisticalYearVo.getSecondQuarter()) || !"--".equals(statisticalYearVo.getThirdQuarter()) || "--".equals(statisticalYearVo.getFourthQuarter())) {
                statisticalYearVo.setYearPlan(yearPlan + "");
            }
        }

        StatisticalYearVo statisticalYearVo1 = statisticalYearVo.calculatebackBreak(statisticalYearVo);


        return statisticalYearVo1;

    }

}